/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"F69914B8-4509-4BA6-9ABC-691A65645D04",variableType:4}
 */
var fConciliado = 2;

/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"A327C33E-88A5-41BA-B989-3A64983C469F",variableType:4}
 */
var fCuenta = null;

/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"6B833ABB-EF14-4A96-82D4-A1BB3C5DCB79",variableType:8}
 */
var fDifSaldos;

/**
 * @type {Date}
 *
 * @properties={typeid:35,uuid:"11A525A5-56DB-45BE-B3B7-F90E6D0E1C56",variableType:93}
 */
var fFecha = null;

/**
 * @type {Date}
 *
 * @properties={typeid:35,uuid:"FA85A8A2-6B28-4DE6-B79B-B48AA66C04BF",variableType:93}
 */
var fFecha_tope = null;

/**
 * @type {Date}
 *
 * @properties={typeid:35,uuid:"1CC2FF17-5E25-4B73-89B1-8F33CFF40430",variableType:93}
 */
var fFechaDesde = null;

/**
 * @type {Date}
 *
 * @properties={typeid:35,uuid:"02B5B87A-FDFC-463D-A1A8-57493777989E",variableType:93}
 */
var fFechaHasta = null;

/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"380693AF-83BE-4F0C-8D88-6EE527434A5E",variableType:8}
 */
var fImporte = null;

/**
 * @type {String}
 *
 * @properties={typeid:35,uuid:"37266E6E-1DA3-4DF6-AF0B-573DEE63C90F"}
 */
var fReferencia = null;

/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"6198769D-A8A6-4F3C-9A75-CDF2282FC89C",variableType:4}
 */
var fValor = null;

/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"E3AABFCF-E36A-48E8-AB28-C693AD0D99F1",variableType:4}
 */
var habilitaFecha = 0;

/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"F8773108-1C9A-440C-8ADC-E2885DD9F32E",variableType:4}
 */
var habilitaImporte = 0;

/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"B9EB78C6-1AD4-4B87-B91E-6FB6421E5A89",variableType:4}
 */
var habilitaRef = 0;

/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"556A9B7B-5252-492C-993B-D6769350315C",variableType:4}
 */
var habilitaValor = 0;

/**
 * Callback method for when form is shown.
 *
 * @param {Boolean} firstShow form is shown first time after load
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"0DC47062-D33E-4BF7-9ECF-67C350CB876E"}
 */
function onShow(firstShow, event) {
	elements.fld_fecha.enabled = 0
	elements.fld_importe.enabled = 0
	elements.fld_referencia.enabled = 0
	elements.fld_valor.enabled = 0
	elements.fld_fecha_desde.enabled = 1
	elements.fld_fecha_hasta.enabled = 1
	elements.fld_cuenta.enabled = 1
	if (globals.gBancoSaldo != null && globals.gBancoSaldo != 0)
	{
		onDataChangeBancoSaldo(); //12/10/2011 para forzar cálculo de fecha tope y control de si está cerrado
	}
	controller.loadRecords(null)
	databaseManager.setAutoSave(true)
}

/**
 * Handle changed data.
 *
 * @param {Object} oldValue old value
 * @param {Object} newValue new value
 * @param {JSEvent} event the event that triggered the action
 *
 * @returns {Boolean}
 *
 * @properties={typeid:24,uuid:"C55EEB0F-D3E3-4762-95F4-6248F37B86C5"}
 */
function onDataChange(oldValue, newValue, event) {
	var elem = 'fld_' + event.getElementName()
	
	elements[elem].enabled = !elements[elem].enabled
	verificaElems()
	return true
}

/**
 * @properties={typeid:24,uuid:"AB9378C6-0D47-4DDC-A675-34176B7E7878"}
 */
function verificaElems()
{
	if (elements.fld_fecha.enabled == 0 && elements.fld_importe.enabled == 0 && elements.fld_referencia.enabled == 0 && elements.fld_valor.enabled == 0)
	{
		elements.fld_fecha_desde.enabled = 1
		elements.fld_fecha_hasta.enabled = 1
		elements.fld_cuenta.enabled = 1
	}
	else
	{
		elements.fld_fecha_desde.enabled = 0
		elements.fld_fecha_hasta.enabled = 0
		elements.fld_cuenta.enabled = 0
	}
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"3E9DC8CA-976C-4974-9F17-829308938990"}
 * @AllowToRunInFind
 */
function buscar(event) {
	if (globals.gCuentaConc != null)
	{
		if (controller.find() == true)
		{
			if (fConciliado == 1)
				saldos_banco_id = globals.gBancoSaldo //25/06/2011 si busca conciliados filtra x extracto
			if (fConciliado != 2)
				marca_conciliacion = fConciliado
			cuentas_cyb_id = globals.gCuentaConc
			if (elements.fld_fecha.enabled == 1)
				fecha_movim = '#'+utils.dateFormat(fFecha,'dd-MM-yyyy')+'|dd-MM-yyyy'
			else
			{
				if (fFechaDesde != null && fFechaHasta != null)
				{
					var Searchdate = new Date()
					Searchdate = utils.dateFormat(fFechaDesde,'dd-MM-yyyy')+'...'+utils.dateFormat(fFechaHasta,'dd-MM-yyyy')
					fecha_movim = '#'+Searchdate +'|dd-MM-yyyy'
				}
				else
				{
					controller.search()
					controller.loadRecords(null)
					globals.DIALOGS.showWarningDialog('Aviso','Por favor indique una fecha','OK')
					return false
				}
			}
		
			if (elements.fld_referencia.enabled == 1)
				referencia = fReferencia
			if (elements.fld_importe.enabled == 1)
				importe = fImporte
			if (elements.fld_valor.enabled == 1)
				valor_id = fValor
				
			var f = controller.search()
			if (f == 0)
				globals.DIALOGS.showWarningDialog('Aviso','No se encontraron resultados','OK')
		}
		else
			globals.DIALOGS.showWarningDialog('Aviso','Ocurrio un error y no pudo completar la operación','OK')
	}
	else
		globals.DIALOGS.showWarningDialog('Aviso','Por favor indique una cuenta','OK')
	return true
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"D731BCB7-1EAD-458A-A713-F8F3A7E0252C"}
 */
function conciliar(event) {
	if (globals.gBancoSaldo == null)
	{
		globals.DIALOGS.showWarningDialog('Aviso','Debe definir período del extracto','OK')
		return
	}
	if (fecha_movim > fFecha_tope)
	{
		globals.DIALOGS.showWarningDialog('Aviso','Fecha posterior al período del extracto','OK')
		return
	}
	if (marca_conciliacion == 0)
	{
		marca_conciliacion = 1
		fecha_conciliacion = new Date()
		fecha_extracto = globals.gFecha_Extracto  //22/01/2011
		if (fecha_extracto < gsaldos_banco_to_saldos_banco.fecha_inicial ||
			fecha_extracto > gsaldos_banco_to_saldos_banco.fecha_final ||
			fecha_extracto == null)
			{
				fecha_extracto = gsaldos_banco_to_saldos_banco.fecha_inicial //25/06/2011
			}
		fDifSaldos-= importe_signo
		saldos_banco_id = globals.gBancoSaldo
		marca_tocado = 1
		//TODO asiento de valores depositados
		// ojo con el asiento - está con autosave=true
	}
	else
		globals.DIALOGS.showWarningDialog('Aviso','Movimiento ya conciliado','OK')
}

/**
 * @properties={typeid:24,uuid:"20718A97-79A3-47E8-8E09-4E834BE314E6"}
 * @AllowToRunInFind
 */
function actualizaDiferencia()
{
	var query = 'SELECT SUM(importe_signo) as "total" '+
			'FROM movim_cyb where ' +
			'marca_conciliacion = 1 and cuentas_cyb_id = ? and saldos_banco_id = ? '+
			'group by cuentas_cyb_id'
	var args = new Array
	args[0] = globals.gCuentaConc
	args[1] = globals.gBancoSaldo
	//args[2] = globals.gUservID // revisar x que no funciona en Logistica
	var $ds = databaseManager.getDataSetByQuery('infocom',query,args,-1)
	
	var total = $ds.getValue(1,1)

	fDifSaldos = gsaldos_banco_to_saldos_banco.saldo_final - gsaldos_banco_to_saldos_banco.saldo_inicial - total
}

/**
 * Handle changed data.
 *
 * @param {Object} [oldValue] old value
 * @param {Object} [newValue] new value
 * @param {JSEvent} [event] the event that triggered the action
 *
 * @returns {Boolean}
 *
 * @properties={typeid:24,uuid:"0DD44F1C-567B-43B2-942C-30DE8D88E9F3"}
 */
function onDataChangeBancoSaldo(oldValue, newValue, event) {
	actualizaDiferencia()
	fFechaDesde = gsaldos_banco_to_saldos_banco.fecha_inicial
	fFechaHasta = gsaldos_banco_to_saldos_banco.fecha_final
	fFecha_tope = gsaldos_banco_to_saldos_banco.fecha_final
	fFecha_tope = fFecha_tope.setHours(23,59,59,999)
	if (gsaldos_banco_to_saldos_banco.marca_cerrado == 1)
	{
		elements.btn_anula.enabled = false
		elements.btn_concilia.enabled = false
	}
	else
	{
		elements.btn_anula.enabled = true
		elements.btn_concilia.enabled = true
	}
	controller.loadRecords(0)
	return true
}

/**
 * Handle hide window.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @returns {Boolean}
 *
 * @properties={typeid:24,uuid:"B60ADA03-67EE-4523-B3D8-C2A1C1C1B969"}
 */
function onHide(event) {
	databaseManager.setAutoSave(false)
	return true
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"30BC49A8-89D0-4DCA-B8AE-B78218398621"}
 */
function anular(event) {
	if (marca_conciliacion == 1)
	{
		marca_conciliacion 	= 0
		fecha_conciliacion 	= null
		fecha_extracto 		= null //22/01/2011
		fDifSaldos			+= importe_signo
		saldos_banco_id 	= null
		marca_tocado 		= 1
		//TODO anular asiento de valores depositados
		// cuidado - está en autosave=true
		
	}
	else
		globals.DIALOGS.showWarningDialog('Aviso','Movimiento NO conciliado','OK')

}

/**
 * Handle changed data.
 *
 * @param {Date} oldValue old value
 * @param {Date} newValue new value
 * @param {JSEvent} event the event that triggered the action
 *
 * @returns {Boolean}
 *
 * @properties={typeid:24,uuid:"E0EE5FEF-EA3C-4AE7-A474-D874A05F5E24"}
 */
function onDataChangeFechaHasta(oldValue, newValue, event) {
	fFecha_tope = newValue
	fFecha_tope = fFecha_tope.setHours(23,59,59,999)
	return true
}

/**
 * Called before the form component is rendered.
 *
 * @param {JSRenderEvent} event the render event
 *
 * @properties={typeid:24,uuid:"6A6D8716-0B7F-44F8-B981-776C534CA7D1"}
 */
function onRender(event) {
	/** @type {JSRecord<db:/infocom/movim_cyb>}*/
	var $rcd = event.getRecord()
	if ($rcd && $rcd.marca_tocado == 1)
	{
		event.getRenderable().fgcolor = '#ff8000'
	}
	else
	{
		event.getRenderable().fgcolor = SM_DEFAULTS.DEFAULT
	}
}

/**
 * Handle changed data.
 *
 * @param {Number} oldValue old value
 * @param {Number} newValue new value
 * @param {JSEvent} event the event that triggered the action
 *
 * @returns {Boolean}
 *
 * @properties={typeid:24,uuid:"1F4CD8B8-5945-48CF-8AD0-F8F47239A64A"}
 */
function onDataChangeBanco(oldValue, newValue, event) {
	controller.loadRecords(0)
	globals.gBancoSaldo = null
	return true
}
